package com.test.controller;

/**
 * Created by Administrator on 2017/4/26.
 */
public class CalculationUtils {

    /**
     * 判断数字的位数
     * @param num
     * @return
     */
    public static int numberOfBits(int num){
        int count = 0;
        if (num >= 0  && num<=Integer.MAX_VALUE) {   //根据实际的数据类型
            while (num != 0) {
                count++;
                num /= 10;
            }
        }
        System.out.println("它是个"+ count+"位的数！");
        return count;
    }

    /**
     * 最大正数
     * @param max
     * @param num
     * @return
     */
    public static int equalization(int max, int num){

        int quotientT = (int) Math.pow(10, num-1);
        int temp = (int) Math.pow(10, num-2);

        //商
        int quotient = max/quotientT;
        //余数
        int remainder = max % quotientT;

        int center = 0;
        if(remainder != 0){
            if(remainder <= 5*temp){
                center = 5*temp;
            }else {
                center = 10*temp;
            }
        }

//        System.out.println("max: "+max+" 位数 "+num+" 中间值 "+ (quotient*quotientT + center));
        return quotient*quotientT + center;

    }

    /**
     * 最小负数
     * @param min
     * @param equalization
     * @return
     */
    public static int negativeNumber(int min, int equalization){
        //商
        int quotient = min/equalization;
        //余数
        int remainder = min % equalization;

        if(remainder == 0){
            return quotient * equalization;
        }else {
            return (quotient + 1) * equalization;
        }
    }
}
